package com.mybpm.core

import com.easecurity.core.basis.UserDo
import com.mybpm.core.definition.ProcessLoadService
import com.mybpm.core.instance.TaskService
import com.mybpm.core.instance.ToDoTask
import com.mybpm.core.utils.ServletUtils
import com.mybpm.core.vo.TaskSubmit
import grails.gorm.transactions.Transactional
import org.springframework.beans.factory.annotation.Autowired

/**
 * 任务处理
 */
//@CompileStatic
class TaskController {

    @Autowired
    TaskService taskService
    @Autowired
    BpmUserService userService
    @Autowired
    ProcessLoadService processLoadService

    /**
     * 启动流程并获取下一环节的候选人列表
     */
    @Transactional
    def start() {
        Map data = ServletUtils.getRequestBodyData()
        UserDo userDo = userService.getCurrentUserDo()
        if (!data.processId) data.processId = processLoadService.processName2Id(data.processName)
        respond taskService.startProcessWithCandidates(data.processId, userDo, data.form), formats: ['json']
    }

    /**
     * 获取下一环节的候选人列表
     */
    def getNextCandidates() {
        Map data = ServletUtils.getRequestBodyData()
        UserDo userDo = userService.getCurrentUserDo()
        ToDoTask task = ToDoTask.get(data.taskId)
        respond taskService.getNextCandidates(task, userDo, data.form), formats: ['json']
    }

    /**
     * 提交下一步处理。如果没有taskId，则默认起草并提交，此时processId不能为null
     */
    @Transactional
    def submit2Next() {
        Map data = ServletUtils.getRequestBodyData()
        UserDo userDo = userService.getCurrentUserDo()
        if (!data.processId) data.processId = processLoadService.processName2Id(data.processName)
        ToDoTask task = data.taskId ? ToDoTask.get(data.taskId) : taskService.startProcess(data.processId, userDo, data.form)
        respond taskService.submit2Next(task, userDo, TaskSubmit.map2Node((List) data.nodeList), data.form), formats: ['json']
    }
}
